package com.foogui.web;

import com.foogui.service.MallService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class MallController {

    @Autowired
    private MallService mallService;

    /**
     * 购买下单，模拟全局事务提交
     * @param count
     * @return
     */
    @RequestMapping(value = "/purchase/commit/{count}")
    public ResponseEntity<String> purchaseCommit(@PathVariable("count") Integer count) {
        try {
            mallService.purchase("1220708537638920191", "C201901140001", count);
        } catch (Exception exx) {
            return new ResponseEntity<>(exx.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
        }
        return ResponseEntity.ok("全局事务提交");
    }

    @RequestMapping(value = "/purchase/commit2/{count}")
    public ResponseEntity<String> purchaseCommit2(@PathVariable("count") Integer count) {
        try {
            mallService.purchaseOfDubbo("1220708537638920191", "C201901140001", count);
        } catch (Exception exx) {
            return new ResponseEntity<>(exx.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
        }
        return ResponseEntity.ok("全局事务提交");
    }
}
